package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Sysnumber;
import com.wzq.service.ISysnumberService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/sysnumber")
public class AdminSysnumberController {

    @Autowired
    ISysnumberService iSysnumberService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Sysnumber> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Sysnumber> pageResult = iSysnumberService.page(page, new QueryWrapper<Sysnumber>().like(StringUtil.isNotEmpty(query), "number", query));
        Map<String,Object> map=new HashMap<>();
        map.put("SysnumberList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

//    @RequestMapping("/save")
//    public R save(@RequestBody Sysnumber sysnumber){
//        if(sysnumber.getId()==null || sysnumber.getId()==-1){
//            iSysnumberService.save(sysnumber);
//        }else{
//            iSysnumberService.saveOrUpdate(sysnumber);
//        }
//        return R.ok();
//    }

    @RequestMapping("/save")
    public R save(@RequestBody Sysnumber sysnumber){
        try {
            if(sysnumber.getId()==null || sysnumber.getId()==-1){
                LambdaQueryWrapper<Sysnumber> lqw = new LambdaQueryWrapper<>();
                lqw.likeRight(Sysnumber::getNumber,sysnumber.getNamebegin());
                int count = iSysnumberService.count(lqw);
                // 生成数量
                for (Integer i=count+1 ; i<=count + sysnumber.getLen() ; i++){
                    StringBuilder zeros =new StringBuilder();
                    for (int j=0; j< 4 - Integer.toString(i).length() ; j++)
                        zeros.append("0");
                    sysnumber.setNumber(sysnumber.getNamebegin() + zeros + i);
                    iSysnumberService.save(sysnumber);
                }
                return R.ok("修改成功");
            }else{
                iSysnumberService.saveOrUpdate(sysnumber);
                return R.ok("添加成功");
            }
        }catch (Exception e) {
            return R.error("操作失败!");
        }
    }



    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Sysnumber sysnumber = iSysnumberService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("sysnumber",sysnumber);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iSysnumberService.removeById(id);
            return R.ok();
        }
    //   添加邀请账号数据
    @GetMapping("/add/{len}")
    public void autoadd(@PathVariable Integer len, @RequestParam String namebegin ) {
        Sysnumber sys=new Sysnumber();
        sys.setLen(len);
        sys.setNamebegin(namebegin);

        //内部测试
        for (Integer i=1;i<= sys.getInsidelen();i++){
            StringBuilder zeros =new StringBuilder();
            for (int j=0; j<len.toString().length() - i.toString().length() ; j++)
                zeros.append("0");
            Sysnumber sysnumber = new Sysnumber();
            sysnumber.setNumber( sys.getNamebegin() +zeros+ i);
            iSysnumberService.save(sysnumber);
        }
        //学生分配
        for (Integer i=sys.getOutbeginlen() ; i<=sys.getLen() ; i++){
            StringBuilder zeros =new StringBuilder();
            for (int j=0; j<len.toString().length() - i.toString().length() ; j++)
                zeros.append("0");
            Sysnumber sysnumber = new Sysnumber();
            sysnumber.setNumber(sys.getNamebegin() + zeros + i);
            iSysnumberService.save(sysnumber);
        }
    }

}

